//
//  SLFriendAddViewController.m
//  StarLight
//
//  Created by 吴涛 on 16/6/10.
//  Copyright © 2016年 Duke.Wu. All rights reserved.
//

#import "SLFriendAddViewController.h"
#import "STPopup.h"
#import "SLFriendManager.h"


@interface SLFriendAddViewController ()<UITextViewDelegate>

@property (nonatomic, strong) SLFriendManager *friendMg;

@property (nonatomic, strong) UITextView *noteTF;

@property (nonatomic, strong) UILabel *tipLabel;

@property (nonatomic, strong) UIButton *confirmBtn;

@property (nonatomic, strong) UIButton *cancelBtn;

@property (nonatomic, assign) NSInteger userId;

@end

@implementation SLFriendAddViewController


- (instancetype)init{
  if (self = [super init]) {
    self.contentSizeInPopup = CGSizeMake(300, 200);
    self.landscapeContentSizeInPopup = CGSizeMake(300, 200);
  }
  return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
  [self.view addSubview:self.tipLabel];
  [self.view addSubview:self.noteTF];
  [self.view addSubview:self.confirmBtn];
  [self.view addSubview:self.cancelBtn];
  [self addConstrains];
    // Do any additional setup after loading the view.
}


- (void)setModel:(SLOperbuddyModel *)model{
  _model = model;
  self.tipLabel.text = [NSString stringWithFormat:@"确认添加 %@ 为好友?",model.name];
  self.userId = model.userId;
}

- (void)setBoatModelModel:(SLBoatMoreModel *)boatModelModel{
  _boatModelModel = boatModelModel;
  self.tipLabel.text = [NSString stringWithFormat:@"确认添加 %@ 为好友?",boatModelModel.name];
  self.userId = boatModelModel.shipOwnerId;
}

- (void)setGoodsMoreModel:(SLMoreGoodsModel *)goodsMoreModel{
  self.tipLabel.text = [NSString stringWithFormat:@"确认添加 %@ 为好友?",goodsMoreModel.name];
  self.userId = goodsMoreModel.hid;
}

- (void)confirmAction{
  
  if (self.noteTF.text.length < 1) {
    [SVProgressHUD showImage:nil status:@"请填写备注信息"];
    return;
  }
  
  if (self.noteTF.text.length > 50) {
    [SVProgressHUD showImage:nil status:@"备注消息最长50字"];
    return;
  }
  
  
  @weakify(self)
  [self.friendMg addFriendWithHuid:self.userId opers:@"0" bz:self.noteTF.text block:^{
    @strongify(self)
    [SVProgressHUD showSuccessWithStatus:@"添加成功"];
    [[NSNotificationCenter defaultCenter] postNotificationName:kAddFriendSuccess object:nil];
    [self cancelAction];
  }];
  
}


- (void)cancelAction{
  [self dismissViewControllerAnimated:YES completion:nil];
}




- (void)addConstrains{
  
  
  [self.tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.centerX.equalTo(self.view);
    make.top.equalTo(self.view).offset(15);
  }];
  
  [self.noteTF mas_makeConstraints:^(MASConstraintMaker *make) {
    make.width.equalTo(self.view).offset(-40);
    make.center.equalTo(self.view);
    make.height.equalTo(@40);
  }];
  
  [self.confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
    make.right.equalTo(self.view.mas_centerX).offset(-20);
    make.width.equalTo(@100);
    make.height.equalTo(@40);
    make.bottom.equalTo(self.view).offset(-4);
  }];
  
  [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.view.mas_centerX).offset(20);
    make.width.equalTo(@100);
    make.height.equalTo(self.confirmBtn);
    make.bottom.equalTo(self.confirmBtn);
  }];
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
  if (range.location>=20){
    [SVProgressHUD showImage:nil status:@"已达输入上限"];
    return  NO;
  }

  return YES;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(SLFriendManager *)friendMg{
  if (!_friendMg) {
    _friendMg = [[SLFriendManager alloc] init];
  }
  return _friendMg;
}


- (UITextView *)noteTF{
  if (!_noteTF) {
    _noteTF = [[UITextView alloc] init];
    _noteTF.backgroundColor = [UIColor lightGrayColor];
    _noteTF.delegate = self;
  }
  return _noteTF;
}

- (UILabel *)tipLabel{
  if (!_tipLabel) {
    _tipLabel = [UILabel labelWithFontSize:16 textColor:[UIColor blackColor] text:@"确认添加好友"];
  }
  return _tipLabel;
}

- (UIButton *)cancelBtn{
  if (!_cancelBtn) {
    _cancelBtn = [UIButton buttonWithTitle:@" 取消 "];
    _cancelBtn.backgroundColor = SLYellowColor;
    _cancelBtn.layer.cornerRadius = 5;
    [_cancelBtn addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
    [_cancelBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  }
  return _cancelBtn;
}

- (UIButton *)confirmBtn{
  if (!_confirmBtn) {
    _confirmBtn = [UIButton buttonWithTitle:@" 确定 "];
    _confirmBtn.backgroundColor = SLYellowColor;
    _confirmBtn.layer.cornerRadius = 5;
    [_confirmBtn addTarget:self action:@selector(confirmAction) forControlEvents:UIControlEventTouchUpInside];
    [_confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  }
  return _confirmBtn;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
